package com.qd.panda.wallet.plan;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.constant.ConstantDto;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.common.panda.domain.entity.wallet.plan.PlanDTO;
import com.qd.panda.service.wallet.plan.PlanService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 钱包/套餐/WEB_充值套餐
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/wallet/plan/")
@Slf4j
public class PlanController {
    private final PlanService planService;

    /**
     * 添加
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:plan:save')")
    @OperatorInfo(title = "添加", operator = OperatorType.INSERT)
    @PostMapping(value = "/save")
    public PlanDTO save(@Validated({DefaultGroup.class}) @RequestBody PlanDTO dto) {
        dto.setAddTime(LocalDateTime.now());
        if (!StringUtils.hasLength(dto.getEnable())) {
            dto.setEnable(ConstantDto.SF1);
        }
        dto.setOperator(SecurityUtils.getLoginUser().getUserId());
        dto.setOperateOrgId(SecurityUtils.getLoginUser().getCurrentOrgId());
        return planService.save(dto);
    }

    /**
     * 删除
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:plan:delete')")
    @OperatorInfo(title = "删除", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            planService.removeById(ids.get(0));
        } else {
            planService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public PlanDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return planService.getByIdDto(id);
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<PlanDTO> page(PlanDTO dto) {
        return planService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 全部
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/list")
    public List<PlanDTO> list(PlanDTO dto) {
        return planService.getListDto(Optional.ofNullable(dto).orElse(new PlanDTO()));
    }

    /**
     * 开启关闭
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:plan:enable')")
    @OperatorInfo(title = "开启关闭", operator = OperatorType.OTHER)
    @PutMapping(value = "/enable")
    public ApiResultWrapper<Object> enable(@Validated @RequestBody PlanDTO dto) {
        if (!StringUtils.hasLength(dto.getEnable())) {
            return ApiResult.error("是否启用不能为空");
        }
        if (!StringUtils.hasLength(dto.getPlanCode())) {
            return ApiResult.error("套餐编码不能为空");
        }
        dto.setUpdateTime(LocalDateTime.now());
        dto.setOperator(SecurityUtils.getLoginUser().getUserId());
        dto.setOperateOrgId(SecurityUtils.getLoginUser().getCurrentOrgId());
        planService.enable(dto);
        return ApiResult.success();
    }
}